package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.RestoreDao;
import dao.impl.RestoreDaoImpl;
import model.Restore;
import model.User;

/**
 * Servlet implementation class ReplyServlet
 */
@WebServlet("/ReplyServlet")
public class ReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReplyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		//提取前端表单中的信息
		String text = request.getParameter("replytext");
		String annID = request.getParameter("annid");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("logoner");
		String userID = user.getUserId();
		//把表单中的信息封装到model中
		Restore restore = new Restore();
		restore.setAnnID(Long.parseLong(annID));
		restore.setRestoreBody(text);
		restore.setUserID(userID);
		restore.setRestoreTime(new Date());
		//访问数据访问层操作数据库
		RestoreDao rd = new RestoreDaoImpl();
		try {
			if(rd.insertRestore(restore) > 0) {
				response.sendRedirect("ReadServlet?ID="+annID);
			}else {
				response.sendRedirect("pages/comment.jsp");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
